/*
 * Copyright(c) 2018-2018, YuanYue Group, Inc. All rights reserved.
 *
 * This software is the confidential and proprietary information of YuanYue Group, Inc.
 * You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with YuanYue Group.
 */
package net.wsfcp.android.ui.view.my.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.wsfcp.android.R;
import net.wsfcp.android.base.BaseActivity;
import net.wsfcp.android.event.LoginEvent;
import net.wsfcp.android.http.mvp.presenter.LoginPresenter;
import net.wsfcp.android.ui.view.my.fragment.LoginFragment;
import net.wsfcp.android.ui.view.my.fragment.RegisterFragment;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * LoginActivity.java是全网覆盖平台的。
 *
 * @author admin
 * @version $Id: LoginActivity.java, v 0.1 2018-12-14 16:45 admin Exp $$
 */

//登陆页面
public class LoginActivity extends BaseActivity {

    @BindView(R.id.rl_left)
    RelativeLayout rlLeft;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.btn_right)
    Button btnRight;

    @BindView(R.id.rl_switch_login)
    RelativeLayout rlSwitchLogin;
    @BindView(R.id.txt_switch_login)
    TextView txtSwitchLogin;
    @BindView(R.id.rl_switch_register)
    RelativeLayout rlSwitchRegister;
    @BindView(R.id.txt_switch_register)
    TextView txtSwitchRegister;
    @BindView(R.id.vp)
    ViewPager vp;

    LoginFragment loginFragment;
    RegisterFragment registerFragment;
    private ArrayList<Fragment> fragmentList = new ArrayList<>();

    @Override
    protected LoginPresenter loadPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected void initData() {
        setmTitle("登录/注册");
        setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


//        mPresenter.getImageCode();
    }

    @Override
    protected void initListener() {

//        txtLogin.setOnClickListener(this);
//        txt_register.setOnClickListener(this);
//        txtFind.setOnClickListener(this);
//        imgCode.setOnClickListener(this);

        rlSwitchLogin.setOnClickListener(this);
        rlSwitchRegister.setOnClickListener(this);
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                checkPage(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    public void checkPage(int position) {

        switch (position) {
            case 0:
                rlSwitchLogin.setBackground(getResources().getDrawable(R.drawable.bg_login_switch_blue));
                txtSwitchLogin.setTextColor(getResources().getColor(R.color.white));

                rlSwitchRegister.setBackground(null);
                txtSwitchRegister.setTextColor(getResources().getColor(R.color.color_time));

                loginFragment.getImgCode();
                break;

            case 1:
                rlSwitchLogin.setBackground(null);
                txtSwitchLogin.setTextColor(getResources().getColor(R.color.color_time));

                rlSwitchRegister.setBackground(getResources().getDrawable(R.drawable.bg_login_switch_blue));
                txtSwitchRegister.setTextColor(getResources().getColor(R.color.white));

                registerFragment.getImgCode();
                break;


        }

    }


    @Override
    protected void initView() {
        btnLeft = rlLeft;
        mTitle = txtTitle;
        btRight = btnRight;

        initFragments();
//        if (!TextUtils.isEmpty(SpUtils.getInstance().getUserName())) {
//            edtUsername.setText(SpUtils.getInstance().getUserName());
//            if (!getIntent().getBooleanExtra("forget", false)) {
//                edt_password.setText(SpUtils.getInstance().getPassword());
//            }
//            edtCode.requestFocus();
//        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        switch (vp.getCurrentItem()) {
            case 0:
                loginFragment.getImgCode();
                break;

            case 1:
                registerFragment.getImgCode();
                break;
        }

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void otherViewClick(View view) {
        switch (view.getId()) {
            case R.id.rl_switch_login:
                vp.setCurrentItem(0);
                break;
            case R.id.rl_switch_register:
                vp.setCurrentItem(1);
                break;


        }

    }


    //初始化页面
    private void initFragments() {
        loginFragment = new LoginFragment();
        registerFragment = new RegisterFragment();

        fragmentList.clear();

//        if (getIntent().getBooleanExtra("forget", false)) {
//                edt_password.setText(SpUtils.getInstance().getPassword());
//            }

        fragmentList.add(loginFragment);
        fragmentList.add(registerFragment);
        //ViewPager设置适配器
        myFragmentPagerAdapter adapter = new myFragmentPagerAdapter(getSupportFragmentManager(), fragmentList);
        vp.setAdapter(adapter);
        //ViewPager显示第一个Fragment
        vp.setCurrentItem(0);
        //ViewPager页面切换监听
    }


    public class myFragmentPagerAdapter extends FragmentPagerAdapter {
        ArrayList<Fragment> list;

        public myFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> list) {
            super(fm);
            this.list = list;
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }


    //接收订阅的事件
    @Subscribe(threadMode = ThreadMode.POSTING)
    public void login(LoginEvent event) {
        switch (event.getCode()) {
            case 0:   // 退出登录
                finish();
                break;
            case 1:   // 登录
                finish();
                break;
            case 2:   // 忘记密码
                finish();
                break;

        }

    }
}